package com.jinxin.platform.cdcardpassmgr.controller;

import com.jinxin.platform.base.common.pojo.JsonResult;
import com.jinxin.platform.cdcardpassmgr.bean.CdcardpassmgrCard;
import com.jinxin.platform.cdcardpassmgr.common.CommonCodeEntity;
import com.jinxin.platform.cdcardpassmgr.common.ResultEntity;
import com.jinxin.platform.cdcardpassmgr.rpc.CdcardpassmgrRpc;
import com.jinxin.platform.cdcardpassmgr.service.ICardpassmgrCardService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

/**
 * All rights Reserved, Designed By www.beonelot.com
 *
 * @version: V1.0
 * @Copyright: www.beonelot.com Inc. All rights reserved.
 * @Description：
 * @Date: 2020-12-26
 * @Author: Lingnan
 **/
@RestController
@RequestMapping("/cardpassmgrCard")
@Slf4j
public class CardpassmgrCardController {
    @Autowired
    private ICardpassmgrCardService cardService;
    @Autowired
    private CdcardpassmgrRpc cdcardpassmgrRpc;

    @RequestMapping("/list")
    public Map<String,Object> list(@RequestParam int page, @RequestParam int limit,String projectId,
                                   String cardNum,String typeId,String carNum,String principal){
        HashMap param = new HashMap();
        param.put("page", page);
        param.put("limit", limit);
        param.put("projectId", projectId);
        param.put("cardNum", cardNum);
        param.put("typeId", typeId);
        param.put("carNum", carNum);
        param.put("principal", principal);
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("code",0);
        resultMap.put("msg","");
        resultMap.put("count", cardService.listPageCount(param));
        resultMap.put("data", cardService.listPage(param));
        return resultMap;
    }

    @PostMapping("/saveOrUpdate")
    public JsonResult saveOrUpdate(@RequestBody CdcardpassmgrCard card){
        return cardService.saveOrUpdate(card);
    }

    @GetMapping("/delete")
    public JsonResult delete(String id){
        return cardService.delete(id);
    }

    @GetMapping("/getDictItems")
    public JsonResult getDictItems(String dictType){
        if(StringUtils.isEmpty(dictType)){
            return null;
        }
        return cdcardpassmgrRpc.getDictItems(dictType);
    }
    @RequestMapping("/deleteAll")
    public JsonResult deleteAll(String params){
        return cardService.deleteAll(params);
    }

    @PostMapping("/uploadCard")
    public ResultEntity uploadCard(MultipartFile file, String projectId,String projectName) {
        if(file == null){
            return new ResultEntity(CommonCodeEntity.FAIL,"请选择文件上传","" );
        }
        if(StringUtils.isEmpty(projectId)){
            return new ResultEntity(CommonCodeEntity.FAIL,"所属项未选择","" );
        }
        try {
            return cardService.uploadCard(file,projectId,projectName);
        } catch (Exception e) {
            log.error("error",e);
            return new ResultEntity(CommonCodeEntity.FAIL,"导入失败，请联系管理员","" );
        }
    }
}
